### OpenCV 설치
#!pip install opencv-python
### easygui 설치
#pip install easygui
### Packages
import cv2
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
#from easygui import fileopenbox, filesavebox
### 출력 영상 크기
plt.rcParams["figure.figsize"] = (16,9) # 그림(figure)의 크기. (가로,세로) 인치 단위
### 파일 선택
img_file = r'D:\image\sample-color.png'
#img_file = fileopenbox()
img_file
'D:\\image\\sample-color.png'
### 영상 읽기
img_raw = cv2.imread(img_file)
### 영상 확인
if img_raw is None:
print("Could not read the image.")
else:
print(img_raw.shape)
(512, 768, 3)
### OpenCV window
cv2.imshow("Display window", img_raw)
cv2.waitKey(0)
cv2.destroyAllWindows()
### 영상 출력 - color
img_rgb = cv2.cvtColor(img_raw, cv2.COLOR_BGR2RGB)
plt.imshow(img_rgb)
plt.show()
### Color 영상을 Grayscale 영상으로 변환
img_gray = cv2.cvtColor(img_raw, cv2.COLOR_BGR2GRAY)
img_gray.shape
(512, 768)
### 영상 출력 - gray
plt.imshow(img_gray, 'gray')
plt.show()
### 저장할 파일명 및 확장자
out_file = r'D:\image\sample-gray.png'
cv2.imwrite(out_file, img_gray)
True